<?php
	if(!defined("ROOT_PATH")) exit("die Access ");
	class user_addressControl extends skymvc{
		
		public function __construct(){
			parent::__construct();	
		}
		
		public function onInit(){
			M("login")->checkLogin();
			$this->userid=M("login")->userid;
		}
		
		public function onDefault(){
		
			$where=" siteid=".SITEID." AND status<10 AND  userid=".$this->userid;
			$url="/index.php?m=user_address&a=default";
			$limit=20;
			$start=get("per_page","i");
			$option=array(
				"start"=>intval(get_post('per_page')),
				"limit"=>$limit,
				"order"=>" isdefault DESC,id DESC",
				"where"=>$where
			);
			$rscount=true;
			$data=M("user_address")->select($option,$rscount);
			
			if($data){
				foreach($data as $v){
					$d_ids[]=$v['province_id'];
					$d_ids[]=$v['city_id'];
					$d_ids[]=$v['town_id'];
				}
				$dist_list=M("site_city")->id_title(array("where"=>" sc_id in(".implode(",",$d_ids).") ","start"=>0,"limit"=>1000000)); 
			}
			$pagelist=$this->pagelist($rscount,$limit,$url);
			$sdata=array(
					"data"=>$data,
					"pagelist"=>$pagelist,
					"rscount"=>$rscount,
					"url"=>$url,
					"dist_list"=>$dist_list
			);
			if(get('ajax')){
				$this->goALl("success",0,$sdata);
			}
			$this->smarty->assign(
				$sdata
			);
			$tpl=M("pagetpl")->get("user_address","index");
			$this->smarty->display($tpl);
			$this->smarty->display("user_address/index.html");
		}
		
		 
		public function onAdd(){
			$id=get_post("id","i");
			if($id){
				$data=M("user_address")->selectRow("id={$id} AND siteid=".SITEID." AND userid=".M("login")->userid);
				if(empty($data)) $this->goall("数据不存在",1);
				$city_list=M("site_city")->id_title(array("where"=>" siteid=".SITEID." AND pid=".$data['province_id']." "));
				$town_list=M("site_city")->id_title(array("where"=>" siteid=".SITEID." AND pid=".$data['city_id']." "));
			} 
				
			 
			$province_list=M("site_city")->id_title(array("where"=>" siteid=".SITEID." AND pid=0 "));
			$bdata=array(
				"data"=>$data,
				"province_list"=>$province_list,
				"city_list"=>$city_list,
				"town_list"=>$town_list,
				"site"=>M("sites")->selectRow("siteid=".SITEID),
				"referer"=>$_SERVER['HTTP_REFERER'],
			);
			
			if(get('ajax')){
				$this->goAll("success",0,$bdata);
			}else{
				$this->smarty->assign($bdata);
				$tpl=M("pagetpl")->get("user_address","add");
				$this->smarty->display($tpl);
				$this->smarty->display("user_address/add.html");
			}
		}
		
		 
		
		public function onSave(){
			
			$id=get_post("id","i");
			$data["userid"]=$this->userid;
			$data["address"]=get_post("address","h");
			$data["telephone"]=get_post("telephone","h");
			$data["truename"]=get_post("truename","h");
			 
			$data["province_id"]=get_post("province_id","i");
			if(empty($data['province_id'])){
				$this->goall("省份不能为空",1);
			}
			$data["city_id"]=get_post("city_id","i");
			$data["town_id"]=get_post("town_id","i");
			$data["dateline"]=time();
			$data['sushe']=post('sushe','i');
		 	$data['isdefault']=post('isdefault','i');
			$data['siteid']=SITEID; 
			if($data['isdefault']){
				M("user_address")->update(array(
					"isdefault"=>0
				),"siteid=".SITEID." AND userid=".M("login")->userid);
			}
			if($id){
				$row=M("user_address")->selectRow("id={$id} AND userid=".M("login")->userid);
				if(empty($row)){
					$this->goall("数据出错",1);				
				}
				M("user_address")->update($data,array('id'=>$id));
			}else{
				$id=M("user_address")->insert($data);
			}
			$data['id']=$id;
			$referer=post('referer','x');
			if(get('ajax')){
				$data['province']=M("site_city")->selectOne(array("where"=>"sc_id=".$data['province_id'],"fields"=>"title"));
				$data['city']=M("site_city")->selectOne(array("where"=>"sc_id=".$data['city_id'],"fields"=>"title"));
				$data['town']=M("site_city")->selectOne(array("where"=>"sc_id=".$data['town_id'],"fields"=>"title"));
				 
				$this->goAll("保存成功",0,$data);
			}else{
				$this->goall($this->lang["save_success"],0,0,$referer);
			}
		}
		
		public function onDelete(){
			$id=get_post('id',"i");
			M("user_address")->update(array("status"=>11),"id={$id} AND userid=".M("login")->userid);
			exit(json_encode(array("error"=>0,"message"=>"删除成功")));
		}
		
		
	}

?>